package gd.hq.yolov5.yolo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import gd.hq.yolov5.Box;
import gd.hq.yolov5.YOLOv5;
import gd.hq.yolov5.caculate.CalculateMotion;
import gd.hq.yolov5.caculate.PersonData;
import gd.hq.yolov5.camera.CameraActivity;

import java.util.ArrayDeque;
import java.util.Queue;

public class Yolo {
    Context mContext;
    public Yolo(Context context) {
        this.mContext = context;
        init();
    }

    public void init() {
        YOLOv5.init(mContext.getAssets());
        CalculateMotion.startCalculate();
    }

    public Box[] detect(Bitmap image, float threshold, float nms_threshold) {
        Box[] result = YOLOv5.detect(image, threshold, nms_threshold);
        return result;
    }

    public Bitmap detect_with_box(Bitmap image, float threshold, float nms_threshold) {
        Box[] result = YOLOv5.detect(image, threshold, nms_threshold);
        Bitmap mutableBitmap = image.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(mutableBitmap);
        Paint boxPaint = new Paint();
        boxPaint.setAlpha(200);
        boxPaint.setStyle(Paint.Style.STROKE);
        boxPaint.setStrokeWidth(4 * image.getWidth()/800);
        boxPaint.setTextSize(40 * image.getWidth()/800);
        //画出框框
        for(Box box:result){
            if(box.getLabel().equals("person")){
                boxPaint.setColor(box.getColor());
                boxPaint.setStyle(Paint.Style.FILL);
                canvas.drawText(box.getLabel(),box.x0,box.y0,boxPaint);
                boxPaint.setStyle(Paint.Style.STROKE);
                canvas.drawRect(box.getRect(),boxPaint);
                CalculateMotion.PERSON_DATES.add(new PersonData(box,image.getWidth(),image.getHeight()));
            }
        }
        return mutableBitmap;
    }

}
